package hlhj.fhp.newslib.activitys

import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.newslib.BaseAty
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.adapters.CommentAdp
import hlhj.fhp.newslib.customview.GoLoginDialog
import hlhj.fhp.newslib.javabean.CommentBean
import hlhj.fhp.newslib.network.Apis
import hlhj.fhp.newslib.network.CommentListAction
import hlhj.fhp.newslib.network.LoadingAction
import hlhj.fhp.newslib.utils.ToastUtil
import kotlinx.android.synthetic.main.hlhj_news_head_lo.*
import kotlinx.android.synthetic.main.hlhj_news_list_lo.*
import kotlinx.android.synthetic.main.hlhj_news_lo_comment.*
import java.util.ArrayList

/**
 * Created by Administrator on 2018\6\25 0025.
 */
class CommentAty :BaseAty() {
    private lateinit var adp:BaseQuickAdapter<CommentBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<CommentBean.DataBean>()

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btSend.setOnClickListener {
            if (etContent.text.isEmpty()){
                ToastUtil.toast("评论内容不能为空")
            }else{
                val tmUser = TMSharedPUtil.getTMUser(this)

                Apis.doComment(intent.getIntExtra("id",0),tmUser.member_id,etContent.text.toString(),
                        TMSharedPUtil.getTMToken(this), "")
                        .doOnSubscribe {
                            loadingDialog.show()
                        }
                        .subscribe {
                            data->
                            if (data.code==1){
                                ToastUtil.toast("评论成功")
                                datas.clear()
                                disPatchData()
                            }else if (data.code==0){
                                GoLoginDialog(this).show()
                            }else{
                                ToastUtil.toast(data.message)
                            }
                            etContent.setText("")
                        }
            }
        }
    }

    override fun initView() {
        tvTittle.text="全部评论"
        adp= CommentAdp(R.layout.news_comment_item,datas)

        /*adp= object :BaseQuickAdapter<CommentBean.DataBean,BaseViewHolder>(R.layout.comment_item,datas){
            override fun convert(helper: BaseViewHolder?, item: CommentBean.DataBean?) {
                *//*Glide.with(mContext).load(item?.avatar).apply(GlideUtil.loadIMGOption())
                        .into(helper?.getView(R.id.itemIcon)!!)*//*
*//*                helper?.setText(R.id.itemName,item?.uname)
                helper?.setText(R.id.itemTime,item?.add_time)
                helper?.setText(R.id.itemContnet,item?.content)*//*
            }
        }*/
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

        disPatchData()
    }

    private fun disPatchData() {
        Apis.getCommentList(intent.getIntExtra("id",0),TMSharedPUtil.getTMToken(this@CommentAty))
                .doOnSubscribe(LoadingAction(this))
                .subscribe(object : CommentListAction<CommentBean>(this@CommentAty){
                    override fun onResult(t: CommentBean) {
                        loadingDialog.dismiss()
                        if (t.code==1){
                            datas.addAll(t.data)
                            adp.notifyDataSetChanged()
                        }else{
                            ToastUtil.toast(t.message)
                        }
                    }
                })
             /*   .subscribe(object :CustomAction<Response<CommentBean>>{
                    override fun onResult(t: Response<CommentBean>) {
                        loadingDialog.dismiss()
                        val body = t.body()
                        datas.addAll(body.data)
                        adp.notifyDataSetChanged()
                    }
                })*/
    }

    override fun getContentId(): Int {
        return R.layout.aty_comment
    }
}